package com.zjsru.plan2023.oneday;

/**
 * @Author: CookLee
 * @Date: 2023/7/12
 *  交替数字和
 *  输入：n = 521
 * 输出：4
 * 解释：(+5) + (-2) + (+1) = 4
 *\
 * 输入：n = 111
 * 输出：1
 * 解释：(+1) + (-1) + (+1) = 1
 *
 */
public class AlternateDigitSum {
    
    /**
     * 模拟
     *
     * @param n n
     * @return int
     */
    public int alternateDigitSum(int n) {
        int ans = 0;
        int sign = 1;
        while (n > 0) {
            int i = n % 10 * sign;
            ans += i;
            //更改符号
            sign = -sign;
            n = n / 10;
        }
        //最高有效位上的数字分配到正号，结果乘以 −sign
        return ans *  -sign ;
    }
    
    public static void main(String[] args) {
        AlternateDigitSum alternateDigitSum = new AlternateDigitSum();
        int n = 886996;
        System.out.println(alternateDigitSum.alternateDigitSum(n));
    }
}
